<template>
  <div class="login-container">
    <!-- 图片 -->
    <div class="login-img" />
    <!-- 登陆表单 -->
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <!-- 手机号 -->
      <el-form-item prop="loginName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.loginName"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!-- 验证码 -->
      <el-form-item prop="code">
        <el-row :gutter="0">
          <el-col :span="17">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input v-model="loginForm.code" placeholder="请输入密码" />
          </el-col>
          <!-- 右侧图片 -->
          <el-col :span="7" @click.native="getImg"> <img :src="codeImg" alt="" class="checkImg"></el-col>
        </el-row>
      </el-form-item>
      <!-- 登陆按钮 -->
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; margin-bottom: 30px"
        @click.native.prevent="handleLogin"
      >
        登陆
      </el-button>
      <!-- 提示 -->
      <div class="tips">
        <span style="margin-right: 20px">username: admin</span>
        <span> password: any</span>
      </div>
    </el-form>
  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import { getCodeAPI } from '@/api'
export default {
  name: 'Login',
  data() {
    return {
      // 表单数据
      loginForm: {
        clientToken: '', // 随机数
        code: '1234',
        loginName: 'admin',
        loginType: 0,
        password: 'admin'
      },
      // 表单验证规则
      loginRules: {
        loginName: [{ required: true, trigger: 'blur', message: '必填' }],
        password: [{ required: true, trigger: 'blur', message: '必填' }],
        code: [
          { required: true, trigger: 'blur', message: '必填' },
          { min: 4, max: 4, message: '验证码4位', trigger: 'blur' }
        ]
      },
      // 按钮状态
      loading: false,
      // 密码框显示与隐藏
      passwordType: 'password',
      // 不知道--好像跟路由有关
      redirect: undefined,
      // 验证码图片
      codeImg: '',
      // 随机数
      random: null
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() { this.getImg() },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    // 登陆触发得函数
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          const res = await this.$store.dispatch('user/login', this.loginForm)
          this.loading = false
          console.log('login', '--', res)
          // 如果失败了  返回错误信息
          if (!res.success) {
            return this.$message.error(res.msg)
          }
          // 成功得话
          this.$message.success(res.msg)
          this.$router.push('/')
        } else {
          console.log('error submit!!')
          return false
        }
        this.loading = false
      })
    },
    // 获取验证码
    async getImg() {
      // 随机数
      this.random = Math.floor(Math.random() * 11)
      // 把表单数据得clientToken同步
      this.loginForm.clientToken = this.random
      // 发送请求
      const res = await getCodeAPI(this.random)
      // console.log('getCodeAPI', '--', res)
      // 获取验证码arraybuffer
      // const img =
      //   'data:image/png;base64,' + btoa(new Uint8Array(res).reduce((res, byte) => res + String.fromCharCode(byte), ''))
      // 获取验证码blob
      const img = window.URL.createObjectURL(res)
      this.codeImg = img
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  // background-color: $bg;
  overflow: hidden;
  background-image: url('~@/assets/login_images/background.png');
  background-repeat: no-repeat;
  background-size: cover;
  // background-image: url('~@/assets/login_images/background.png');
  .login-form {
    position: relative;
    width: 518px;
    // height: 388px;
    // left: 50%;
    // top: 50%;
    max-width: 100%;
    padding: 60px 35px 0;
    border-radius: 10px;
    margin: 160px auto;
    background-color: #fff;
    overflow: hidden;
  }
  // 自己加得图片
  .login-img {
    position: absolute;
    width: 96px;
    height: 96px;
    top: 105px;
    left: 50%;
    margin-left: -48px;
    background-image: url(../.././assets/login_images/logo.png);
    background-size: 100%;
    z-index: 1;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
// 新增样式
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  height: 47px;
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.checkImg {
  width: 100%;
  height: 100%;
}
</style>
